package com.woniuxy.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import com.woniuxy.entity.CategoryOne;
import com.woniuxy.entity.CategoryTwo;
import com.woniuxy.entity.Perms;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.Roles;
import com.woniuxy.entity.Secutity;
import com.woniuxy.entity.User;

@Mapper
public interface UserMapper {

	// 注册用户，在把用户信息添加到用户表之后，得到用户id，然后把用户id和角色id添加到用户角色表

	@Insert("insert into user values(default,#{account},#{password},null,#{number},#{phone},#{email},#{head},#{status},#{regtime})")
	public boolean addUser(User user);

	// 注册用户，在把用户信息添加到用户表之后，得到用户id，然后把用户id和角色id添加到用户角色表
	@Insert("INSERT INTO user_roles VALUES((SELECT u_id from user WHERE account = #{account}),2)")
	public boolean addUserRoles(String account);

	@Select("select * from user where u_id=#{uid}")
	public User find(int uid);

	@Select("select password from user where u_id=#{uid}")
	public String findpwdbyuid(int uid);

	@Update("update user set password=#{password} where u_id=#{u_id}")
	public int updatepwdbyuid(User user);

	@Update("update user set number=#{number},gender=#{gender},phone=#{phone},email=#{email} where u_id=#{u_id}")
	public int upinfo(User user);

	@Update("update user set head=#{head} where u_id=#{u_id}")
	public int updateHeadImg(User user);

	// 查找用户的密保答案
	@Select("select answer from secutity where u_id=#{u_id} and question=#{question}")
	public String findanswer(Secutity secutity);

	// 查找用户的所有密保问题
	@Select("select * from secutity where u_id=#{u_id}")
	public List<Secutity> findquestion(int u_id);

	// 根据前端传来的问题和用户ID查找密保id
	@Select("select id from secutity where u_id=#{u_id} and question=#{question}")
	public int findid(Secutity secutity);

	// 修改密保问题
	@Update("update secutity set question=#{question},answer=#{answer} where id=#{id}")
	public int updatesecutity(Secutity secutity);

	// 查找当前用户密保问题的数量
	@Select("select count(*) from secutity where u_id=#{u_id}")
	public int findSecutityCount(int u_id);

	// 增加密保问题
	@Insert("insert into secutity values (default,#{u_id},#{question},#{answer})")
	public int addsecutity(Secutity secutity);

	// 修改用户激活状态
	@Update("updata user set status=#{status},regtime=#{regtime} where u_id=#{u_id}")
	public int updataStatus(int u_id, Date regtime, int status);

	@Select("select*from user")
	public List<User> allUser();

	// 查询用户信息
	@Select("select*from user where u_id=#{u_id}")
	public User findUserByUid(int u_id);

	// 查询用户信息
	@Select("select*from user where account=#{account}")
	public User findByAccount(String account);

	// 查询角色权限
	@Select("select u_id from user where account=#{account}")
	@Results({ @Result(id = true, column = "u_id", property = "u_id"),
			@Result(column = "u_id", property = "roles", many = @Many(select = "findRolesByUid", fetchType = FetchType.LAZY))// 按需加载
	})
	public User findRolesAndPerms(String account);

	// 查询角色
	@Select("select ur.u_id,ur.ro_id,r.type  from user_roles ur,roles r where ur.ro_id=r.ro_id and ur.u_id=#{u_id} ")
	@Results({ @Result(id = true, column = "ro_id", property = "ro_id"),
			@Result(column = "ro_id", property = "perms", many = @Many(select = "findPermsByRid")) })
	public List<Roles> findRolesByUid(int u_id);

	// 查询权限
	@Select("select rp.ro_id,rp.pe_id,p.type from roles_perms rp,perms p where rp.pe_id=p.pe_id and rp.ro_id=#{ro_id}")
	public List<Perms> findPermsByRid(int ro_id);

	// 批量修改用户状态
	@Update({ "<script> UPDATE user SET status = #{status} " + "WHERE u_id IN "
			+ "<foreach  collection = 'ids' item = 'u_id' index = 'index' open = '(' separator= ',' close = ')' >"
			+ "	#{u_id} " + "</foreach>" + "</script>" })
	public int batchUpdateUsers(@Param("status") Integer status, @Param("ids") int[] ids);

	// 批量删除用户
	@Update({ "<script> DELETE FROM user WHERE u_id IN "
			+ "<foreach  collection = 'ids' item = 'u_id' index = 'index' open = '(' separator= ',' close = ')' >"
			+ "	#{u_id} " + "</foreach>" + "</script>" })
	public int batchDeleteUsers(@Param("ids") int[] ids);

}
